<?php
class baoan extends control{
	public function __construct(){
		parent::__construct();
		$this->p = $this->app->loadClass('proving');
	}

	//获取要打印的保安员列表  http://127.0.0.1/baoan/baoan-get_baoan
	public function get_baoan(){

		$name =isset($_REQUEST['name'])?(!empty($_REQUEST['name'])?$_REQUEST['name']:''):'';
		$identity =isset($_REQUEST['identity'])?(!empty($_REQUEST['identity'])?$_REQUEST['identity']:''):'';
		$stataus =(int)@$_REQUEST['stataus'];
		$crtime =isset($_REQUEST['crtime'])?(!empty($_REQUEST['crtime'])?$_REQUEST['crtime']:''):'';//导入时间
		$uptime =isset($_REQUEST['uptime'])?(!empty($_REQUEST['uptime'])?$_REQUEST['uptime']:''):'';//打印时间
        $where ='1=1  and status !=3';
        if($stataus!=3){ $where .= " and status = $stataus ";   }

        if($name){ $where .= " and  name like '%$name%' "; }
        if($identity){  $where .=" and sfz like '%$identity%' ";	}
        if($crtime){$where .= "  and crtime like '%$crtime%' ";	}
        if($uptime){$where .= "  and uptime like '%$uptime%' ";	}


		$tabledata = $this->baoan->get_list($where);
		$tabledata_arr = array();
        //数据处理
		foreach ( $tabledata as $key => $v ) {
			if ($v->status == 1) {
					$tabledata[$key]->status = '<span class="layui-badge-dot layui-bg-green"></span>  已打印';
				} else if ($v->status == 0) {
					$tabledata[$key]->status = '<span class="layui-badge-dot"></span>  未打印';
			}
			$tabledata[$key]->bday = substr($v->sfz, 6, 4).'年'.substr($v->sfz, 10, 2).'月'.substr($v->sfz, 12, 2).'日';
		}
        $this->view->tabledata = json_encode($tabledata);

        $this->view->name = $name;
        $this->view->identity = $identity;
        $this->view->stataus = $stataus;
        $this->view->crtime = $crtime;
        $this->view->uptime =$uptime;
		$this->display();
	}

	public function print_baoan(){
			$id = isset($_REQUEST['id']) ? $_REQUEST['id'] :'';
			if($id==''){//如果id为空
				//查出未打印的 取第一条
				$where = " status='0' order by id asc limit 1";
				$baoandata = $this->baoan->user_data($where);
				if($baoandata==''){ echo '没有未打印的'; return ;}
			}else{
				$where = " id =$id";
				$baoandata = $this->baoan->user_data($where);
				//如果数据为空 去找未打印的取出一条
				if($baoandata==''){
				  	$where = " status='0' and id>$id order by id asc limit 1";
					$baoandata = $this->baoan->user_data($where);
					if($baoandata==''){ echo '没有未打印的'; return ;}
				 }
			}
		    $this->view->baoandata = $baoandata;
			$this->display();
	}
	//更新打印状态
		public function up_date(){
				$upid = isset($_POST['upid'])?(!empty($_POST['upid'])?$_POST['upid']:''):'';
				$baoandata = $this->baoan->user_update($upid );
            return 1;


	}
     // 搜索查询  --没用到
	public function get_baoan1(){
		//header('Content-Type:application/json');
		$name =isset($_POST['name'])?(!empty($_POST['name'])?$_POST['name']:''):'';
		$identity =isset($_POST['identity'])?(!empty($_POST['identity'])?$_POST['identity']:''):'';
		//$stataus =$_POST['stataus'];
		$crtime =isset($_POST['crtime'])?(!empty($_POST['crtime'])?$_POST['crtime']:''):'';//导入时间
		$uptime =isset($_POST['uptime'])?(!empty($_POST['uptime'])?$_POST['uptime']:''):'';//打印时间
        $where ='1=1  and status !=3';
        //if($stataus!=3){ $where .= " and status = $stataus ";   }

        if($name){ $where .= " and  name like '%$name%' "; }
        if($identity){  $where .=" and sfz like '%$identity%' ";	}
        if($crtime){$where .= "  and crtime like '%$crtime%' ";	}
        if($uptime){$where .= "  and uptime like '%$uptime%' ";	}
 		$tabledata = $this->baoan->get_list($where);
  		$ret = [ "msg" => 200,
		       "data" => $tabledata,
		       ];
         $data =  json_encode($ret,JSON_UNESCAPED_UNICODE);
         echo $data;
	}
    //编辑页面
	public function editor(){
	            $id = isset($_REQUEST['id']) ? $_REQUEST['id'] :'';
				if($id ==""){
				echo '请刷新'; exit;
				}
				$where = " id =$id";
				$baoandata = $this->baoan->user_data($where);
			    $this->view->baoandata = $baoandata;
				$this->display();
	}
    //删除
	public function deluser(){
	            $id = isset($_REQUEST['id']) ? $_REQUEST['id'] :'';
				if($id ==""){echo '请刷新'; exit;			}
				$baoandata = $this->baoan->user_deluser($id);
               //1 成功

	}
    //更新信息页面
	public function up_editor(){
	            $id = isset($_POST['date_id']) ? $_POST['date_id'] :'385';
	            $name = isset($_POST['name']) ? $_POST['name'] :'';
	            $fadate = isset($_POST['date']) ? $_POST['date'] :'';
	            $sfz = isset($_POST['sfz']) ? $_POST['sfz'] :'';
	            $papers = isset($_POST['papers']) ? $_POST['papers'] :'';
	            $address = isset($_POST['address']) ? $_POST['address'] :'';

	            if($name){ $where=" name='$name '" ;}
	            if($sfz){ $where .=", sfz='$sfz' " ;}
	            if($fadate){ $where .=", fadate='$fadate' " ;}
	            if($papers){ $where .=", papers='$papers' " ;}
	            if($address){ $where .=", address='$address' " ;}

				$baoandata = $this->baoan->up_editor($where,$id);
    }



    //上传
    public function file_upload(){

        if ($_FILES) {
			require_once '../../../lib/PHPExcel/PHPExcel.class.php';
			require_once '../../../lib/PHPExcel/PHPExcel/IOFactory.php';
			require_once '../../../lib/PHPExcel/PHPExcel/Reader/Excel2007.php';//excel 2007
			$p = $this->p;

			$file = @$_FILES['file_upload']['name'];//文件名字

			$filetempname = @$_FILES['file_upload']['tmp_name'];//临时文件目录

			$ext = substr($file, strrpos($file,'.')+1); //判断文件后缀
			if($ext !='xls'  && $ext !='xlsx'){
				//echo $p->arrayTOjson(array('status'=>'false','remark'=>'文件类型必须是xls格式的文件'));exit;
				echo '上传失败，文件类型必须是xls格式的文件';exit;
			}
			$ty = date('Y',time());
			$tm = date('m',time());
			$td = date('d',time());
			//自己设置的上传文件存放路径
			$filePath = '../../../upload/excel/'.$ty."/".$tm."/".$td."/";

			if (!is_dir($filePath)) { //判断是否创建目录
				mkdir($filePath,0777,true); //创建文件目录
			}

			$filename=explode(".",$file);//把上传的文件名以“.”好为准做一个数组。
			$filename[0]=time().rand(0,99);//取文件名t替换
			$name=implode (".",$filename); //上传后的文件名
			$uploadfiles=$filePath.$name;//上传后的文件名地址


			//move_uploaded_file() 函数 将上传的文件移动到新位置。若成功，则返回 true，否则返回 false。
			$uploadfile = $p->my_iconv($uploadfiles);

			$result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下

			//如果上传文件成功，就执行导入 excel操作
			if($result){

			   $objReader  = PHPExcel_IOFactory::createReader('Excel2007');	//use excel2003 和  2007 format
			   $objPHPExcel = PHPExcel_IOFactory::load($uploadfile);//改成这个写法就好了

			   $sheet = $objPHPExcel->getSheet(0);
			   $highestRow = $sheet->getHighestRow(); // 取得总行数
			   $highestColumn = $sheet->getHighestColumn(); // 取得总列数

			 //  for($j=2;$j<=$highestRow;$j++){
			 //      for($k='A';$k<=$highestColumn;$k++){
			 //          a($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue());
			 //      }
			 //      exit;
			 //  }
			   $data = array();
			     $crtime = date("Y-m-d");
				//循环读取excel文件,读取一条,插入一条
				for($j=2;$j<=$highestRow;$j++){
					$row = (object)array();
					$row->name = $objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();//姓名
					$row->sfz = $objPHPExcel->getActiveSheet()->getCell("B$j")->getValue();//身份证号
					$row->address = $objPHPExcel->getActiveSheet()->getCell("C$j")->getCalculatedValue();//地址
					$row->papers = $objPHPExcel->getActiveSheet()->getCell("D$j")->getValue();//保安证号
					//$row->fadate = $objPHPExcel->getActiveSheet()->getCell("E$j")->getValue();//打印日期
					$row->fadate = date('Y/m/d',\PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("E$j")->getValue()));

					$row->crtime = date("Y-m-d");
					$data[] = $row;
				}

				if(!empty($data)){
					$this->baoan->insert_data($data);
				}

			}
		}
		$this->display();
    }




}
